//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "YoYInflationVolatilityTraits.h"
using namespace Cephei::QL::Experimental::Inflation;
#include <gen/QL/Termstructures/Volatility/Inflation/YoYOptionletVolatilitySurface.h>
using namespace Cephei::QL::Termstructures::Volatility::Inflation;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Inflation::CYoYInflationVolatilityTraits::CYoYInflationVolatilityTraits (boost::shared_ptr<QuantLib::YoYInflationVolatilityTraits>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phYoYInflationVolatilityTraits = NULL;
#endif
	_ppYoYInflationVolatilityTraits = &childNative;
    
}
Cephei::QL::Experimental::Inflation::CYoYInflationVolatilityTraits::CYoYInflationVolatilityTraits (QuantLib::YoYInflationVolatilityTraits& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phYoYInflationVolatilityTraits = NULL;
#endif
	_ppYoYInflationVolatilityTraits = new boost::shared_ptr<QuantLib::YoYInflationVolatilityTraits> (&childNative);
    
    _YoYInflationVolatilityTraitsOwner = owner;
    
}

Cephei::QL::Experimental::Inflation::CYoYInflationVolatilityTraits::CYoYInflationVolatilityTraits (CYoYInflationVolatilityTraits^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phYoYInflationVolatilityTraits = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppYoYInflationVolatilityTraits = new boost::shared_ptr<QuantLib::YoYInflationVolatilityTraits> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Inflation::CYoYInflationVolatilityTraits::CYoYInflationVolatilityTraits (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phYoYInflationVolatilityTraits = NULL;
#endif
	if (!t->IsSubclassOf(CYoYInflationVolatilityTraits::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Inflation::CYoYInflationVolatilityTraits::CYoYInflationVolatilityTraits (QuantLib::Handle<QuantLib::YoYInflationVolatilityTraits>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phYoYInflationVolatilityTraits = &childNative;
	_ppYoYInflationVolatilityTraits = &static_cast<boost::shared_ptr<QuantLib::YoYInflationVolatilityTraits>>(childNative.currentLink());
    
    _YoYInflationVolatilityTraitsOwner = owner;
}
Cephei::QL::Experimental::Inflation::CYoYInflationVolatilityTraits::CYoYInflationVolatilityTraits (QuantLib::Handle<QuantLib::YoYInflationVolatilityTraits> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phYoYInflationVolatilityTraits = &childNative;
	_ppYoYInflationVolatilityTraits = &static_cast<boost::shared_ptr<QuantLib::YoYInflationVolatilityTraits>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Inflation::CYoYInflationVolatilityTraits::CYoYInflationVolatilityTraits (QuantLib::YoYInflationVolatilityTraits childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phYoYInflationVolatilityTraits = NULL;
#endif
	_ppYoYInflationVolatilityTraits = new boost::shared_ptr<QuantLib::YoYInflationVolatilityTraits> (new QuantLib::YoYInflationVolatilityTraits (childNative));
    
}
#endif

Cephei::QL::Experimental::Inflation::CYoYInflationVolatilityTraits::~CYoYInflationVolatilityTraits ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppYoYInflationVolatilityTraits != NULL)
    {
	    delete _ppYoYInflationVolatilityTraits;
        _ppYoYInflationVolatilityTraits = NULL;
    }
}
Cephei::QL::Experimental::Inflation::CYoYInflationVolatilityTraits::!CYoYInflationVolatilityTraits ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppYoYInflationVolatilityTraits != NULL)
    {
	    delete _ppYoYInflationVolatilityTraits;
    }
}
QuantLib::YoYInflationVolatilityTraits& Cephei::QL::Experimental::Inflation::CYoYInflationVolatilityTraits::GetReference ()
{
    if (_ppYoYInflationVolatilityTraits == NULL) throw REFNEW NativeNullException ();
	return **_ppYoYInflationVolatilityTraits;
}
boost::shared_ptr<QuantLib::YoYInflationVolatilityTraits>& Cephei::QL::Experimental::Inflation::CYoYInflationVolatilityTraits::GetShared ()
{
    if (_ppYoYInflationVolatilityTraits == NULL) throw REFNEW NativeNullException ();
	return *_ppYoYInflationVolatilityTraits;
}
QuantLib::YoYInflationVolatilityTraits* Cephei::QL::Experimental::Inflation::CYoYInflationVolatilityTraits::GetPointer ()
{
    if (_ppYoYInflationVolatilityTraits == NULL) throw REFNEW NativeNullException ();
	return &**_ppYoYInflationVolatilityTraits;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::YoYInflationVolatilityTraits>& Cephei::QL::Experimental::Inflation::CYoYInflationVolatilityTraits::GetHandle ()
{
	if (_phYoYInflationVolatilityTraits == NULL)
	{
		_phYoYInflationVolatilityTraits = new Handle<QuantLib::YoYInflationVolatilityTraits> (*_ppYoYInflationVolatilityTraits);
	}
	return *_phYoYInflationVolatilityTraits;
}
#endif
bool Cephei::QL::Experimental::Inflation::CYoYInflationVolatilityTraits::HasNative () 
{
	return (_ppYoYInflationVolatilityTraits != NULL);
}

DateTime Cephei::QL::Experimental::Inflation::CYoYInflationVolatilityTraits::InitialDate (Cephei::QL::Termstructures::Volatility::Inflation::IYoYOptionletVolatilitySurface^ s)
{
    CYoYOptionletVolatilitySurface^ _Cs;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cs = safe_cast<CYoYOptionletVolatilitySurface^> (s);
        _Cs->Lock();
        QuantLib::YoYOptionletVolatilitySurface* _s = static_cast<QuantLib::YoYOptionletVolatilitySurface*> (_Cs->GetPointer ()); 
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppYoYInflationVolatilityTraits)->initialDate ( _s );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cs != nullptr) _Cs->Unlock();
    }
}
Double Cephei::QL::Experimental::Inflation::CYoYInflationVolatilityTraits::InitialValue (Cephei::QL::Termstructures::Volatility::Inflation::IYoYOptionletVolatilitySurface^ s)
{
    CYoYOptionletVolatilitySurface^ _Cs;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cs = safe_cast<CYoYOptionletVolatilitySurface^> (s);
        _Cs->Lock();
        QuantLib::YoYOptionletVolatilitySurface* _s = static_cast<QuantLib::YoYOptionletVolatilitySurface*> (_Cs->GetPointer ()); 
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppYoYInflationVolatilityTraits)->initialValue ( _s );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cs != nullptr) _Cs->Unlock();
    }
}
Cephei::QL::Experimental::Inflation::IYoYInflationVolatilityTraits^ Cephei::QL::Experimental::Inflation::CYoYInflationVolatilityTraits::UpdateGuess (Cephei::Core::IVector<Double>^ vols, Double level, UInt64 i)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        vols->Lock();
        INativeVector<Double>^ _NCIvols = vols->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCvols = safe_cast<CDoubleVector^>(_NCIvols);
        std::vector<QuantLib::Real>& _vols = static_cast<std::vector<QuantLib::Real>&> (_NCvols->GetReference ());
        QuantLib::Real _level = (QuantLib::Real)ValueHelper::Convert (level); //a
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
    	(*_ppYoYInflationVolatilityTraits)->updateGuess ( _vols,  _level,  _i );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (vols != nullptr) vols->Unlock(); //NO2  
    }
}
UInt64 Cephei::QL::Experimental::Inflation::CYoYInflationVolatilityTraits::MaxIterations::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppYoYInflationVolatilityTraits)->maxIterations ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

